package com.ivan.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ivan.feign.cmn.DictFeignClient;
import com.ivan.hospital.mapper.BookingRuleMapper;
import com.ivan.hospital.mapper.DepartmentMapper;
import com.ivan.hospital.mapper.HospitalMapper;
import com.ivan.model.entity.hosp.BookingRule;
import com.ivan.model.entity.hosp.Department;
import com.ivan.model.entity.hosp.Hospital;
import com.ivan.hospital.service.HospitalService;
import com.ivan.model.vo.hosp.HospitalQueryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * @Author      Ivan
 * @Date        2023/4/4 15:36
 * @Description
 */
@Service
public class HospitalServiceImpl extends ServiceImpl<HospitalMapper, Hospital> implements HospitalService {

    @Autowired
    private DictFeignClient dictFeignClient;

    @Autowired
    private BookingRuleMapper bookingRuleMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 医院列表(条件查询分页)
     * @param page
     * @param limit
     * @param hospitalQueryVo
     * @return
     */
    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //创建pageable对象
        Page<Hospital> pageObj = new Page<>(page, limit);
        QueryWrapper<Hospital> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(hospitalQueryVo.getHosname())) {
            wrapper.like("hosname", hospitalQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hospitalQueryVo.getProvinceCode())) {
            wrapper.eq("province_code", hospitalQueryVo.getProvinceCode());
        }
        if (!StringUtils.isEmpty(hospitalQueryVo.getCityCode())) {
            wrapper.eq("city_code", hospitalQueryVo.getCityCode());
        }
        if (!StringUtils.isEmpty(hospitalQueryVo.getDistrictCode())) {
            wrapper.eq("district_code", hospitalQueryVo.getDistrictCode());
        }
        if (!StringUtils.isEmpty(hospitalQueryVo.getHostype())) {
            wrapper.eq("hostype", hospitalQueryVo.getHostype());
        }
        Page<Hospital> hospitalPage = baseMapper.selectPage(pageObj, wrapper);
        //获取查询list集合，遍历进行医院等级封装
        hospitalPage.getRecords().stream().forEach(item -> {
            this.setHospitalHosType(item);
        });
        return hospitalPage;
    }

    /**
     * 更新医院上线状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(String id, Integer status) {
        //根据id查询医院信息
        Hospital hospital = baseMapper.selectById(id);
        //设置修改的值
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        baseMapper.updateById(hospital);
    }

    /**
     * 根据医院的编号查询医院的详情
     * @param hoscode
     * @return
     */
    @Override
    public Map<String, Object> getHospByHoscode(String hoscode) {
        Map<String, Object> result = new HashMap<>();
        Hospital hospital = this.setHospitalHosType(baseMapper.selectHospitalNameByCode(hoscode));
        //医院基本信息（包含医院等级）
        result.put("hospital",hospital);
        //单独处理更直观
        BookingRule bookingRule = bookingRuleMapper.selectByHoscode(hospital.getHoscode());
        bookingRule.setRules(bookingRule.getRule());//设置预约规则
        result.put("bookingRule", bookingRule);
        return result;
    }

    /**
     * 通过医院名称查询信息
     * @param hosname
     * @return
     */
    @Override
    public List<Hospital> findByHosname(String hosname) {
        QueryWrapper<Hospital> wrapper = new QueryWrapper<>();
        wrapper.like("hosname", hosname);
        return baseMapper.selectList(wrapper);
    }

    /**
     * 根据医院的编号查科室
     * @param hoscode
     * @return
     */
    @Override
    public List<Department> findDepartment(String hoscode) {
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        wrapper.like("hoscode", hoscode);
        return departmentMapper.selectList(wrapper);
    }

    //获取查询list集合，遍历进行医院等级封装
    private Hospital setHospitalHosType(Hospital hospital) {
        if(hospital != null) {
            //根据dictCode和value获取医院等级名称
            String hostypeString = dictFeignClient.getNameByValue(hospital.getHostype());
            //查询省 市  地区
            String provinceString = dictFeignClient.getNameByValue(hospital.getProvinceCode());
            String cityString = dictFeignClient.getNameByValue(hospital.getCityCode());
            String districtString = dictFeignClient.getNameByValue(hospital.getDistrictCode());

            hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
            hospital.getParam().put("hostypeString",hostypeString);

            //查询预约规则
            BookingRule bookingRule = bookingRuleMapper.selectByHoscode(hospital.getHoscode());
            hospital.setBookingRule(bookingRule);
        }
        return hospital;
    }
}
